package com.rattsoftware.server.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import com.rattsoftware.server.admin.model.Staff;
import com.rattsoftware.server.admin.model.paging.PagingLoadConfig;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;
import com.rattsoftware.server.dao.StaffDAO;

/**
 * Provides a hibernate implementation
 * of the <code>StaffDAO</code> data access <code> interface.</code>
 */
@Repository("staffDAO")
public class HibernateStaffDAO extends BaseDAOImpl<Staff> implements StaffDAO {

	@Override
	public void deleteStaff(Staff staff) {
		remove(staff);
	}

	@Override
	public List<Staff> getStaff() {
		Search s = new Search();
		s.addSort("name", false);
		return search(s);
	}

	@Override
	public Staff getStaff(Integer id) {
		return super.find(id);
	}

	@Override
	public PagingLoadResult<Staff> getStaffList(PagingLoadConfig loadConfig) {
		Search staffSearch = getSearchFromLoadConfig(loadConfig, "id");
		SearchResult<Staff> result = searchAndCount(staffSearch);
		return getPagingLoadResult(loadConfig, result);
	}

	@Override
	public PagingLoadResult<Staff> getStaffList(Integer deptId, PagingLoadConfig loadConfig) {
		Search staffSearch = getSearchFromLoadConfig(loadConfig, "id");
		staffSearch.addFilterEqual("deptId", deptId);
		SearchResult<Staff> result = searchAndCount(staffSearch);
		return getPagingLoadResult(loadConfig, result);
	}

	@Override
	public void saveStaff(Staff staff) {
		save(staff);
	}

	@Override
	public Staff getStaff(String firstName, String lastName) {
		Search s = new Search();
		s.addFilterEqual("first_name", firstName);
		s.addFilterEqual("last_name", lastName);
		return super.searchUnique(s);
	}
}
